/*
 * This file is part of HE_Mesh, a library for creating and manipulating meshes.
 * It is dedicated to the public domain. To the extent possible under law,
 * I , Frederik Vanhoutte, have waived all copyright and related or neighboring
 * rights.
 * 
 * This work is published from Belgium. (http://creativecommons.org/publicdomain/zero/1.0/)
 * 
 */
package wblut.hemesh;

import java.io.File;
import java.io.PrintWriter;
import wblut.geom.WB_Coord;

/**
 * 
 */
public class HET_WriterPoV {
    /** The comma. */
    final String COMMA = ", ";
    /** The pov writer. */
    protected PrintWriter povWriter;
    /** The num vertices written. */
    protected int numVerticesWritten = 0;
    /** The num normals written. */
    protected int numNormalsWritten = 0;

    /**
     * Handles PrintWriter input.
     *
     * @param path 
     * @param name 
     */
    public void beginSave(final String path, final String name) {
	try {
	    povWriter = new PrintWriter(new File(path, name + ".pov"));
	    handleBeginSave();
	} catch (final Exception e) {
	    e.printStackTrace();
	}
    }

    /**
     * 
     *
     * @param pw 
     */
    public void beginSave(final PrintWriter pw) {
	povWriter = pw;
	handleBeginSave();
    }

    /**
     * End of mesh2 declaration.
     */
    public void endSave() {
	povWriter.println("}");
	povWriter.flush();
    }

    /**
     * Begin the mesh2 output as a PovRAY declaration.
     *
     * @param name
     *            the name
     */
    public void beginMesh2(final String name) {
	final StringBuilder pov = new StringBuilder("#declare ");
	pov.append(name);
	pov.append(" = mesh2{\n");
	pov.append("\tvertex_vectors {");
	povWriter.println(pov);
    }

    /**
     * End the current section ie vertex_vector, normal_vector or face_indices.
     */
    public void endSection() {
	povWriter.println("\t}");
    }

    /**
     * Output start of normal_vectors.
     *
     * @param count
     *            the count
     */
    public void beginNormals(final int count) {
	povWriter.println("\tnormal_vectors{");
	total(count);
    }

    /**
     * Output start of face_indices.
     *
     * @param count
     *            the count
     */
    public void beginIndices(final int count) {
	povWriter.println("\tface_indices{");
	total(count);
    }

    /**
     * Used to output total count vertex_vector, normal_vector & face_indices.
     *
     * @param count
     *            the count
     */
    public void total(final int count) {
	povWriter.println(String.format("\t%d,", count));
    }

    /**
     * Face vertex indices.
     *
     * @param a
     *            the a
     * @param b
     *            the b
     * @param c
     *            the c
     */
    public void face(final int a, final int b, final int c) {
	povWriter.println(buildVector(a, b, c));
    }

    /**
     * Gets the curr normal offset.
     *
     * @return the curr normal offset
     */
    public int getCurrNormalOffset() {
	return numNormalsWritten;
    }

    /**
     * Gets the curr vertex offset.
     *
     * @return the curr vertex offset
     */
    public int getCurrVertexOffset() {
	return numVerticesWritten;
    }

    /**
     * Handle begin save.
     */
    protected void handleBeginSave() {
	povWriter.println("// generated by HE_POVExport");
	numVerticesWritten = 0;
	numNormalsWritten = 0;
    }

    /**
     * New object.
     *
     * @param name
     *            the name
     */
    public void newObject(final String name) {
	povWriter.println(name);
    }

    /**
     * Triangle normals.
     *
     * @param n
     *            the n
     */
    public void normal(final WB_Coord n) {
	povWriter.println(buildVector(n));
	numNormalsWritten++;
    }

    /**
     * Triangle vertices.
     *
     * @param v
     *            the v
     */
    public void vertex(final WB_Coord v) {
	povWriter.println(buildVector(v));
	numVerticesWritten++;
    }

    /**
     * Builds the vector.
     *
     * @param a
     *            the a
     * @param b
     *            the b
     * @param c
     *            the c
     * @return the string builder
     */
    private StringBuilder buildVector(final int a, final int b, final int c) {
	final StringBuilder my_vector = new StringBuilder(120);
	my_vector.append('\t').append('<');
	my_vector.append(a).append(COMMA);
	my_vector.append(b).append(COMMA);
	my_vector.append(c).append('>');
	return my_vector.append(COMMA);
    }

    /**
     * Builds the vector.
     *
     * @param n
     *            the n
     * @return the string builder
     */
    private StringBuilder buildVector(final WB_Coord n) {
	final StringBuilder my_vector = new StringBuilder(120);
	my_vector.append('\t').append('<');
	my_vector.append(n.xd()).append(COMMA);
	my_vector.append(n.yd() * -1).append(COMMA);
	my_vector.append(n.zd() * -1).append('>');
	return my_vector.append(COMMA);
    }
}